package pl.edu.kassiopeia.repository.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class PluginDescriptorConverter {
	
	Element root = null;
	
	public PluginDescriptorConverter(String pluginDescriptor) throws SAXException, IOException, ParserConfigurationException{
		InputStream is = new ByteArrayInputStream(pluginDescriptor.getBytes("UTF-8"));
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = factory.newDocumentBuilder().parse(is);
		root = doc.getDocumentElement();
		System.out.println(root.getNodeName());
	}
	
	public String getName(){
		NodeList nodeList = root.getElementsByTagName("name");
		for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return node.getFirstChild().getNodeValue();
            }
        }
		return "";
	}
	
	public String getGroupId(){
		NodeList nodeList = root.getElementsByTagName("groupId");
		for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return node.getFirstChild().getNodeValue();
            }
        }
		return "";
	}
	
	public String getPluginId(){
		NodeList nodeList = root.getElementsByTagName("pluginId");
		for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return node.getFirstChild().getNodeValue();
            }
        }
		return "";
	}
	
	public String getVersion(){
		NodeList nodeList = root.getElementsByTagName("version");
		for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return node.getFirstChild().getNodeValue();
            }
        }
		return "";
	}
	
	public String getDescription(){
		NodeList nodeList = root.getElementsByTagName("description");
		for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return node.getFirstChild().getNodeValue();
            }
        }
		return "";
	}
	
	public String getAuthor(){
		NodeList nodeList = root.getElementsByTagName("author");
		for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return node.getFirstChild().getNodeValue();
            }
        }
		return "";
	}
	
	public String getEntryPoint(){
		NodeList nodeList = root.getElementsByTagName("entry-point");
		for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return node.getFirstChild().getNodeValue();
            }
        }
		return "";
	}
	
	public List<HashMap<String, String>> getConfiguration(){
		return null;
	}

}
